package cn.edu.neusoft.dnui.startproject.model;


import android.widget.Toast;

import cn.edu.neusoft.dnui.startproject.MainActivity;
import cn.edu.neusoft.dnui.startproject.bean.PhoneResult;
import cn.edu.neusoft.dnui.startproject.iface.PhoneIface;
import cn.edu.neusoft.dnui.startproject.listener.PhoneListener;
import cn.edu.neusoft.dnui.startproject.service.PhoneService;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by Junsheng on 2016/12/14.
 */

public class PhoneModel extends BaseModel implements PhoneIface {
    private PhoneService service;
    private static PhoneModel model;
    private static final String BASE_URL = "http://apis.baidu.com/";
    private static final String API_KEY = "8e13586b86e4b7f3758ba3bd6c9c9135";
    public PhoneModel(String url) {
        super(url);
    }
    public PhoneModel(){
        super(BASE_URL);
    }
    public static PhoneModel getInstance(){
        if(model==null){
            model = new PhoneModel();

        }
        return model;
    }

    @Override
    public void getLocation(String tel, final PhoneListener listener) {
        service = createService(PhoneService.class);
        Call<PhoneResult> call = service.getResult(API_KEY,tel);
        call.enqueue(new Callback<PhoneResult>() {
            @Override
            public void onResponse(Call<PhoneResult> call, Response<PhoneResult> response) {
                if(response.isSuccessful()&&response.body().getErrNum()==0){
                    listener.onResponse(response.body().getRetData().getCity());
                }
                else{
                    listener.onFail("解析错误！");
                }
            }

            @Override
            public void onFailure(Call<PhoneResult> call, Throwable t) {
                listener.onFail(t.toString());
            }
        });
    }
}
